﻿using Caliburn.Micro;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Phones.Client.ViewModel
{
    public class TreeViewItemBaseViewModel : PropertyChangedBase
    {
        #region Data fields
        protected string _imageSourceCollapsed = "/Style/blue-folder.png";
        protected string _imageSourceExpanded = "/Style/blue-folder-open.png";
        static readonly TreeViewItemBaseViewModel DummyChild = new TreeViewItemBaseViewModel();
        readonly ObservableCollection<TreeViewItemBaseViewModel> _children;
        readonly TreeViewItemBaseViewModel _parent;
        bool _isExpanded;
        bool _isSelected;
        #endregion // Data fields

        #region _CTORs
        protected TreeViewItemBaseViewModel(TreeViewItemBaseViewModel parent, bool lazyLoadChildren)
        {
            _parent = parent;

            _children = new ObservableCollection<TreeViewItemBaseViewModel>();

            if (lazyLoadChildren)
                _children.Add(DummyChild);
        }

        private TreeViewItemBaseViewModel() { }
        #endregion // _CTORs

        #region Presentation
        /// <summary>
        /// Returns the logical child items of this object.
        /// </summary>
        public ObservableCollection<TreeViewItemBaseViewModel> Children
        {
            get { return _children; }
        }

        /// <summary>
        /// Returns true if this object's Children have not yet been populated.
        /// </summary>
        public bool HasDummyChild
        {
            get { return this.Children.Count == 1 && this.Children[0] == DummyChild; }
        }

        /// <summary>
        /// Gets/sets an image for the node
        /// </summary>
        public virtual string ImageSource
        {
            get { return IsExpanded ? _imageSourceExpanded : _imageSourceCollapsed; }
        }

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is expanded.
        /// </summary>
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    NotifyOfPropertyChange(() => IsExpanded);
                    NotifyOfPropertyChange(() => ImageSource);
                }

                // Expand all the way up to the root.
                if (_isExpanded && _parent != null)
                    _parent.IsExpanded = true;

                // Lazy load the child items, if necessary.
                if (this.HasDummyChild)
                {
                    this.Children.Remove(DummyChild);
                    this.LoadChildren();
                }
            }
        }

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is selected.
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    NotifyOfPropertyChange(() => IsSelected);
                }
            }
        }

        public TreeViewItemBaseViewModel Parent
        {
            get { return _parent; }
        }

        /// <summary>
        /// Invoked when the child items need to be loaded on demand.
        /// Subclasses can override this to populate the Children collection.
        /// </summary>
        protected virtual void LoadChildren()
        {
            //NotifyOfPropertyChange(() => Children);
        }
        #endregion // Presentation
    }
}
